import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import Immutable from 'immutable';
import {GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN, GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS, GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_BEGIN:
            return state.merge({
                isFetching: true,
            });
        case actions.GET_INIT_SUCCESS:
            return state.merge({
                isFetching: false,
                isDealerUser: action.data.isDealerUser,
                brands: action.data.brands,
            });
        case actions.GET_INIT_FAIL:
            return state.merge({
                isFetching: false,
            });
        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
            });
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                data: action.data.content
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                data: []
            });
        default:
            return state;
    }
};

const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.merge(action.obj);
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        default:
            return state;
    }
};

const pageTableCondition = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const ui = (state, action) => {
    switch(action.type) {
        case actions.START_BATCH_RECEIVE:
            return state.merge({
                showBatchReceivePanel: true,
                showBatchThawPanel: false
            });
        case actions.CLEAR_BATCH_DATA:
            return state.set('showBatchReceivePanel', false);
        case actions.START_BATCH_THAW:
            return state.merge({
                showBatchReceivePanel: false,
                showBatchThawPanel: true
            });
        case actions.CLEAR_BATCH_THAW:
            return state.set('showBatchThawPanel', false);
        default:
            return state;
    }
};

const domainData = combineReducers({
    initData,
    list,
    permission
});

const appState = combineReducers({
    queryCondition,
    pageTableCondition
});

import {createNotificationReducer as notification} from 'Shared/utils/serverNotification';

export default combineReducers({
    domainData,
    appState,
    notification,
    ui
});


